package tests;

import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import daotests.UsersManagement;


public class TestDao {
	@Test
	public void testThatHibernameWorks() {
		String contextFile1="classpath:bankapp-test-persistence-beans.xml";
		ClassPathResource cpres1 = new ClassPathResource(contextFile1);
		
		Assert.assertNotNull(cpres1);
		Assert.assertTrue(cpres1.isReadable());
		
		String contextFile2="classpath:bankapp-test-spring-dao-beans.xml";
		ClassPathResource cpres2 = new ClassPathResource(contextFile2);
		
		Assert.assertNotNull(cpres2);
		Assert.assertTrue(cpres2.isReadable());
		
		ClassPathXmlApplicationContext repositoryContext = new ClassPathXmlApplicationContext(new String[] {contextFile1, contextFile2});
		
		UsersManagement userManagement = repositoryContext.getBean("usersManagement", UsersManagement.class);
		 
		Assert.assertNotNull(userManagement);
		Assert.assertNotNull(userManagement.getGenericDao());
		
		List<User> users = userManagement.getUsers();
		Assert.assertNotNull(users);
		
		if (users.size() == 0) {
			User user = new User();
			user.setId((long)0);
			user.setName("foo");
			userManagement.getGenericDao().saveOrUpdate(user);
		}
		
	}
	
	
}
